<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<style>
    .layui-checkbox-disbaled[lay-skin=primary] span {
        color: rgba(99, 99, 99, 0.89);
    }

    #ly-form .layui-form .layui-form-label {
        width: 70px !important;
    }

    #remark-input {
        min-width: 350px !important;
    }

    .layui-table-click {
        background-color: #add3c2 !important;
    }
</style>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add"> 添加用户</button>
            </div>
        </script>

        <table class="layui-hide" id="operatorUser" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">更改角色</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#operatorUser',
            url: '/getOperator',
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            parseData:function(msg){
                for (let i = 0; i < msg.data.length; i++) {
                    msg.data[i].role=`${msg.data[i].role}`.replace(1,"超级管理员").replace(2,"管理员").replace(3,"普通用户");
                }
                return msg;
            },
            cols: [
                [
                    {field: 'id', width: 100, title: '序号', sort: true},
                    {field: 'name', width: 150, title: '用户名'},
                    {field: 'nickname', width: 150, title: '姓名'},
                    {field: 'parent', width: 150, title: '部门', sort: true},
                    {field: 'role', width: 150, title: '角色'},
                    {field: 'remark', width: 300, title: '备注', sort: true},
                    {title: '操作', minWidth: 100, toolbar: '#currentTableBar', fixed: "right", align: "center"}
                ]
            ],
            limits: [5, 10],
            limit: 5,
            page: true
        });

        $("#operatorInfo").hide();

        function ableEditOperatorInfo() {
            $("#operatorInfo").show();
            $("input").prop("disabled", false);
            $(".layui-input-block input").prop("disabled", false);
            $("#editBtn").show();
        }

        function showOperatorInfo() {
            $("#editBtn").hide();
            $("#operatorInfo").show();
            $("input").prop("disabled", true);
            $(".layui-input-block input").prop("disabled", true);
        };


        //监听行单击事件（）
        table.on('row(currentTableFilter)', function (obj) {
            // let data = obj.data;
            //单行点击后显示操作员信息
            // showOperatorInfo();
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });
            //执行搜索重载
            table.reload('operatorUser', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });
        /**
         * toolbar监听事件
         */
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                let index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/enterprise/addOperator',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });


        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            //console.log(data.id);
            if (obj.event === 'edit') {
                let index = layer.open({
                    title: '编辑权限',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: ['/enterprise/addOperator?id=' + data.id, 'no'],//传值过去，通过id查值修改
                    // success: function(layero, index){
                    //     //少了这个是不能从父页面向子页面传值的
                    //     let body=layer.getChildFrame('body',index);
                    //     //获取子页面的元素，进行数据渲染
                    //     body.contents().find("#name").val(data.name);
                    //     body.contents().find('#passwd').val(data.passwd);
                    //     body.contents().find('#nickname').val(data.nickname);
                    //     body.contents().find('#remark').val(data.remark);
                    //     let id = '#role' + data.role;
                    //     body.contents().find(id).prop("checked", true);//这是子页面的单选按钮，让其选中
                    // }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                ableEditOperatorInfo();
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url:`/deleteOperator`,
                        type:`post`,
                        dataType:`json`,
                        data: {id:data.id},
                        success:function (res) {
                            console.log(res);
                            if (res.code == 200){
                                layer.msg('删除成功');
                                obj.del();
                                layer.close(index);
                                window.parent.location.reload();
                            }else{
                                layer.msg('删除失败');
                            }
                        }
                    });
                    return false;
                });
            }
        });
    });
</script>
<script>
</script>
</body>
</html>